import cache from '../../utils/cache.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    statusList: [
    ],
    functionList: [
      { title: "分享推荐", sub_title: "向好友推荐医数聚" },
      { title: "客服/帮助", sub_title: "在线客服、意见反馈" },
      { title: "安全中心", sub_title: "手机绑定、修改密码" },
      { title: "系统设置", sub_title: "新消息通知方式、语言" }
    ],
    orderTotal: 20,
    imgUrl: '', // 头像地址
    type: '', // 职位
    isRegist: 1, //  注册状态 0 未注册、 1 已注册
    isLogin: 1, //  登录状态 0 未登录、 1 已登录
    identStatus: 0 // 认证状态 0 未认证、 1 待审核、 2 已认证
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let insApp = getApp()
    if (!insApp.loginQuery(true)) return
    console.log(wx.getStorageSync('userInfo'))
      let userInfo = wx.getStorageSync('userInfo')
      if (!userInfo.id){
        return
      }
      this.getOrdersNum(userInfo.id)
      let identStatus
      // 认证状态
      userInfo.verify == 1 ? (identStatus = 2) :
        userInfo.verify == 2 ? (identStatus = 1) :
         userInfo.verify == 3 ? (identStatus = 0) : (identStatus = 1)
      let imgUrl = wx.envConfig.filePath + '/' + userInfo.avatar_name
      let type
      userInfo.type == 0 ? (type = '医生') :
        userInfo.type == 1 ? (type = '代理商') :
         userInfo.type == 2 ? (type = '医学生') : ''
      this.setData({
        userInfo: userInfo,
        identStatus: identStatus,
        imgUrl: imgUrl,
        type: type
      })
    // this.getOrdersNum(wx.getStorageSync('userInfo').id)
    // var appInstance = getApp()
    // appInstance.getAppUserInfo(true).then((res)=>{
    //   console.log('用户信息---------',res)
    //   let userInfo = res.data.datas
    //   this.getOrdersNum(userInfo.id)
    //   let identStatus
    //   // 认证状态
    //   userInfo.verify == 1 ? (identStatus = 2) :
    //     userInfo.verify == 2 ? (identStatus = 1) :
    //      userInfo.verify == 3 ? (identStatus = 0) : ''
    //   let imgUrl = wx.envConfig.imgHost + '/' + userInfo.avatar_name
    //   let type
    //   userInfo.type == 0 ? (type = '医生') :
    //     userInfo.type == 1 ? (type = '代理商') :
    //      userInfo.type == 2 ? (type = '医学生') : ''
    //   this.setData({
    //     userInfo: userInfo,
    //     identStatus: identStatus,
    //     imgUrl: imgUrl,
    //     type: type
    //   })
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    cache.set('canShowModal', true)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    getApp().getAppUserInfo(true).then(res => {
      this.onShow();
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },
  imgOnError () { // 图片显示问题
    this.setData({
      imgUrl: "../../resource/icon/Me/Unselect@2x.png"
    })
  },
  toDownloadApp () { // 去下载列表
    var url = './../mine/downloadApp/downloadApp'
    console.log(url)
    wx.navigateTo({
      url: url
    })
  },
  /**
   * 跳转到用户信息页面
   */
  gotoUserInfo: function (event) {
    this.login();
  },
  // 获取订单数量
  getOrdersNum (id) {
    wx.showLoading({title: '正在加载'})
    wx.request({
      url: wx.envConfig.host + '/order/getDoctorOrderStatusCount?params=' + JSON.stringify({user_doctor_id: id}),
      method: 'POST',
      success: (res) => {
        wx.hideLoading()
        console.log('res', res.data.datas.datas)
        let data = res.data.datas.datas
        let statusList = [
          { title: "待付款", icon_path: "../../resource/icon/waiting-pay2x.png", bardge: data.pending_pay },
          { title: "设计中", icon_path: "../../resource/icon/designing2x.png", bardge: data.design },
          { title: "生产中", icon_path: "../../resource/icon/tool2x.png", bardge: data.product },
          { title: "待收货", icon_path: "../../resource/icon/waiting-receive3x.png", bardge: data.pending_goods },
          { title: "待评价", icon_path: "../../resource/icon/waiting-evalute2x.png", bardge: data.pending_evaluate },
          { title: "已完成", icon_path: "../../resource/icon/finished2x.png", bardge: data.complete }
        ]

        this.setData({
          statusList: statusList,
          orderTotal: data.count
        })
      },
      fail: (err) => {

      }
    })
  },
  /**
   * 点击列表项
   */
  tapFunction: function (event) {
    var func = event.currentTarget.dataset.func;
    var title = func.title;
    wx.showToast({
      title: title,
    })
    if ("分享推荐" == title) {

    }
    if ("客服/帮助" == title) {

    }
    if ("安全中心" == title) {

    }
    if ("系统设置" == title) {

    }
  },
  /**
  * 点击进入订单详情
  */
  goOrderList: function (event) {
    var index = event.currentTarget.dataset.index
    var url = './../order/orderList/orderList?index='+index;
    console.log(url)
    wx.navigateTo({
      url: url
    })
  },
  /**
   * 去登录
   */
  login: function () {
    var url = './regist/regist';
    console.log(url);
    wx.navigateTo({
      url: url
    })
  },
  /**
   * 进入个人信息页面
   */
  goPersonalInformation: function () {
    var url = './personalInformation/personalInformation';
    wx.navigateTo({
      url: url
    })
  },
  onTabItemTap (item) {
    console.log('点击了我的页的导航栏')
    return
  }

})